package com.top.art.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.top.art.bo.BaseBO;
import com.top.art.bo.RulerBO;
import com.top.art.bo.RulerQueryBO;
import com.top.art.converter.RulerConverter;
import com.top.art.entity.Ruler;
import com.top.art.entity.StudioInfo;
import com.top.art.exception.ArtException;
import com.top.art.mapper.RulerMapper;
import com.top.art.service.IRulerService;
import com.top.art.tool.AuthTool;
import com.top.art.vo.CourseVO;
import com.top.art.vo.PaperScoreVO;
import com.top.art.vo.ProvinceVO;
import com.top.art.vo.ResultList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.top.art.tool.Tool.notNullAndEmpty;

/**
 * The type Ruler manager.
 *
 * @author hjh
 * @version 2020 /10/7-16:53
 */
@Component
public class RulerManager {

    @Autowired
    private IRulerService rulerService;

    @Resource
    private RulerConverter rulerConverter;

    @Autowired
    private RulerMapper rulerMapper;

    /**
     * Create boolean.
     *
     * @param bo the bo
     * @return the boolean
     */
    public boolean create(RulerBO bo) {
        Ruler ru = rulerService.getOne(new QueryWrapper<Ruler>().lambda().eq(Ruler::getProvince, bo.getProvince()));
        if (ru != null) {
            throw new ArtException(500, "省份已存在");
        }
        String account = AuthTool.currentUser().getAccount();
        Ruler ruler = rulerConverter.bo2do(bo);
        ruler.setCreator(account);
        ruler.setModifier(account);
        return rulerService.save(ruler);
    }

    /**
     * Query ruler bo.
     *
     * @param province the province
     * @return the ruler bo
     */
    public RulerBO query(String province) {
        return rulerConverter.do2bo(rulerService.getOne(Wrappers.<Ruler>lambdaQuery().eq(notNullAndEmpty(province), Ruler::getProvince, province)));
    }

    /**
     * Update boolean.
     *
     * @param bo the bo
     * @return the boolean
     */
    public boolean update(RulerQueryBO bo) {
        String account = AuthTool.currentUser().getAccount();
        Ruler ruler = rulerConverter.bo2do(bo);
        ruler.setModifier(account);
        return rulerService.updateById(ruler);
    }

    public List<ProvinceVO> queryProvince() {
        return rulerMapper.queryProvince();
    }

    public ResultList<RulerBO> query(BaseBO bo) {
        ResultList<RulerBO> ret = new ResultList<>();
        PageHelper.startPage(bo.getCurrent(), bo.getSize());
        List<Ruler> rulers = rulerService.list();
        if (rulers != null && !rulers.isEmpty()) {
            PageInfo pageInfo = new PageInfo(rulers);
            List<RulerBO> list = rulers.stream().map(rulerConverter::do2bo).collect(Collectors.toList());
            ret.setList(list);
            ret.setTotal(pageInfo.getTotal());
            ret.setPageNum(bo.getCurrent());
            ret.setPageSize(bo.getSize());
        } else {
            ret.setTotal(0L);
            ret.setPageNum(bo.getCurrent());
            ret.setPageSize(bo.getSize());
        }
        return ret;
    }

    public List<CourseVO> queryCourse(String province) {
        return rulerService.queryCourse(province);
    }

}
